package com.mdp.workflow.client.service;

import com.mdp.msg.client.PushMsgService;
import com.mdp.msg.client.PushNotifyMsgService;
import com.mdp.safe.client.entity.User;
import com.mdp.workflow.api.FlowMsgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FlowMsgServiceImpl implements FlowMsgService {
	@Autowired
	PushNotifyMsgService notifyMsgService;

	@Autowired
	PushMsgService pushMsgService;

	 @Value("${mdp.platform-branch-id:platform-branch-001}")
	 String platformBranchId="";

	@Override
	public Map<String, Object> pushMsg(User sendUser, String toUserid, String toUsername, String msg, String url) {
		Map<String,Object> result=notifyMsgService.pushMsg(sendUser,toUserid,toUsername,msg,url);
		pushMsgService.pushMsgToIm(result);
		return result;
	}
	
	@Override
	public void pushMsg(String toUserid, String msg) {
		String branchId=platformBranchId;
		String sendUsername="平台管理员";
		String sendUserid=platformBranchId;
		String toUsername="";
		this.pushMsg(branchId,sendUserid,sendUsername,toUserid,toUsername,msg);
		
	}
	public void pushMsg(String branchId, String sendUserid, String sendUsername, String toUserid, String toUsername, String msg) {

		Map<String,Object> map=new HashMap<>();
		map.put("toUserid", toUserid);
		map.put("toUsername", toUsername);
		map.put("sendUserid", sendUserid);
		map.put("sendUsername", sendUsername);
		map.put("action", "newMessage");
		map.put("msgType", "prichat");
		map.put("subTypes","0,1");
		map.put("sendContent", msg);
		map.put("store", "1");
		map.put("bizType","wfTask");
		map.put("branchId",branchId);
		notifyMsgService.pushMsg(map);
		pushMsgService.pushMsgToIm(map);

	}
	@Override
	public void pushTaskMsg(String toUserid, String mainTitle, String taskName, String commentMsg, String bizUrl) {
		List<String> l=new ArrayList<>();
		String msg="新任务待您处理";
		l.add(msg);
		if(!StringUtils.isEmpty(mainTitle)) {
			 l.add("流程:【"+mainTitle+"】");
		}
		if(!StringUtils.isEmpty(taskName)) {
			 l.add("任务:【"+taskName+"】");
		}

		if(!StringUtils.isEmpty(commentMsg)) {
			l.add("意见建议:"+commentMsg+"");
		}

		if(!StringUtils.isEmpty(bizUrl)) {
			l.add("链接:"+bizUrl);
		}  
		msg=StringUtils.join(l.toArray(),",");
		this.pushMsg(toUserid, msg);
		
	}

}
